package com.hlkj.baojin.service.impl;

import com.hlkj.baojin.dao.IUserDao;
import com.hlkj.baojin.entity.User;
import com.hlkj.baojin.jwt.JwtUser;
import com.hlkj.baojin.service.IUserService;
import com.hlkj.baojin.utils.ErrororUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;


/**
 * 用户验证方法
 *
 * @author hackyo
 * Created on 2017/12/8 9:18.
 */
//@CacheConfig(cacheNames = "userServiceImpl")
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao userDAO;


    static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public IUserDao getUserDao() {
        return this.userDAO;
    }

    @Override
    public User getByUsername(String username) {
        return userDAO.findByUsername(username);
    }
    @Override
    public User getByTypeIsAdmin() {
        return userDAO.findByType(2);
    }
    @Override
    public JwtUser getCurrentUser(){
        try {
            return (JwtUser) SecurityContextHolder.getContext()
                    .getAuthentication()
                    .getPrincipal();
        }catch (Exception e){
            ErrororUtil.runThrow(null, "获取用户", "获取当前用户信息失败");
            return null;
        }
    }

}


